﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using Results.Models;

namespace Results.ViewModels
{
    /// <summary>
    /// Groups, like "Men 14-16"
    /// </summary>
    public class GroupsViewModel : ViewModelBase
    {
        /// <summary>
        /// Groups, like "Men 14-16"
        /// </summary>
        public ObservableCollection<GroupViewModel> Groups { get; private set; }

        /// <summary>
        /// Populate with all the unique groups found the results
        /// </summary>
        /// <param name="resultsModel">Source data to build from</param>
        /// <param name="notifySelection">Methods to call if the user selects something</param>
        public GroupsViewModel(IEnumerable<ResultModel> resultsModel, INotifySelection notifySelection)
        {
            Groups = new ObservableCollection<GroupViewModel>();

            foreach (var group in
                from resultModel in resultsModel
                group resultModel by resultModel.Group into grouping
                orderby grouping.Key
                select new { Group = grouping.Key })
            {
                Groups.Add(new GroupViewModel(group.Group, notifySelection));
            }
        }
    }
}
